package com.loongtech.bi.action.system;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.loongtech.bi.entity.system.EntitySysRole;
import com.loongtech.bi.entity.system.EntitySysUser;
import com.loongtech.bi.entity.system.EntitySysUserRole;
import com.loongtech.bi.manager.system.SysRoleManager;
import com.loongtech.bi.manager.system.SysUserManager;
import com.loongtech.bi.manager.system.SysUserProjectRoleManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.faces.context.FacesContext;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色用户设置
 * 
 * @author xckuang
 * @date 2014-10-07
 */
@Controller("roleUserAction")
@Scope("view")
public class RoleUserAction implements Serializable {
	private static final long serialVersionUID = 1L;
	@Autowired
	private SysUserManager sysUserManager;
	@Autowired
	private SysUserProjectRoleManager sysUserProjectRoleManager;
	@Autowired
	private SysRoleManager sysRoleManager;
	private Map<Integer, Boolean> selected = new HashMap<Integer, Boolean>();
	private Map<Integer, Boolean> unselected = new HashMap<Integer, Boolean>();
	private Map<Integer, Boolean> selectedMap;
	private List<EntitySysRole> roleList = new ArrayList<EntitySysRole>();
	private List<EntitySysUser> leftList = new ArrayList<EntitySysUser>();
	private List<EntitySysUser> rightList = new ArrayList<EntitySysUser>();

	@Autowired
	public void init() {
		String param = FacesContext.getCurrentInstance().getExternalContext()
				.getRequestParameterMap().get("selectedList");
		JSONArray obj = (JSONArray) JSONObject.parse(param);
		selected.clear();
		unselected.clear();
		roleList.clear();
		if (null != obj && !obj.isEmpty()) {
			for (int index = 0; index < obj.size(); ++index) {
				EntitySysRole role = sysRoleManager.get(obj.getIntValue(index));
				if (null != role) {
					roleList.add(role);
				}
			}
		}
		leftList.clear();
		leftList.addAll(sysUserManager.findAll());
		rightList.clear();
	}

	public void select() {
		for (EntitySysUser user : leftList) {
			if (selected.get(user.getId())) {
				rightList.add(user);
			}
		}
		selected.clear();
		leftList.removeAll(rightList);
	}

	public void unSelect() {
		for (EntitySysUser user : rightList) {
			if (unselected.get(user.getId())) {
				leftList.add(user);
			}
		}
		unselected.clear();
		rightList.removeAll(leftList);
	}

	/**
	 * 保存角色用户信息
	 */
	public void save() {
		if (null == roleList || roleList.isEmpty()) {
			return;
		}
		if (null == rightList || rightList.isEmpty()) {
			return;
		}
		// 判断role是否还存在
		for (java.util.Iterator<EntitySysRole> ir = roleList.iterator(); ir.hasNext();) {
			EntitySysRole role = ir.next();
			if (null == sysRoleManager.get(role.getId())) {
				ir.remove();
			}
		}
		// 保存
		for (EntitySysUser user : rightList) {
			int userId = user.getId();
			// 判断user是否存在
			if (null == sysUserManager.get(userId)) {
				continue;
			}
			for (EntitySysRole role : roleList) {
				int roleId = role.getId();
				if (null == sysUserProjectRoleManager.getBy(EntitySysUserRole.K.roleId, roleId, EntitySysUserRole.K.userId, userId)) {
					EntitySysUserRole entity = new EntitySysUserRole();
					entity.setRoleId(roleId);
					entity.setUserId(userId);
					sysUserProjectRoleManager.persist(entity);
				}
			}
		}
	}

	public List<EntitySysRole> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<EntitySysRole> roleList) {
		this.roleList = roleList;
	}

	public Map<Integer, Boolean> getSelected() {
		return selected;
	}

	public void setSelected(Map<Integer, Boolean> selected) {
		this.selected = selected;
	}

	public Map<Integer, Boolean> getUnselected() {
		return unselected;
	}

	public void setUnselected(Map<Integer, Boolean> unselected) {
		this.unselected = unselected;
	}

	public Map<Integer, Boolean> getSelectedMap() {
		return selectedMap;
	}

	public void setSelectedMap(Map<Integer, Boolean> selectedMap) {
		this.selectedMap = selectedMap;
	}

	public List<EntitySysUser> getLeftList() {
		return leftList;
	}

	public void setLeftList(List<EntitySysUser> leftList) {
		this.leftList = leftList;
	}

	public List<EntitySysUser> getRightList() {
		return rightList;
	}

	public void setRightList(List<EntitySysUser> rightList) {
		this.rightList = rightList;
	}
}